package org.lab.metricmodel;

import java.io.IOException;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import org.lab.bean.ModelBean;
import org.lab.service.UrlForward;
import org.lab.xmlparser.Xmlbuilder;

public class Modeling extends HttpServlet {

	private Logger log = Logger.getLogger(this.getClass());
	/**
	 * Constructor of the object.
	 */
	public Modeling() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		ModelBean bean = (ModelBean)request.getSession().getAttribute("modelBean");
		//获取导向资源文件
		ResourceBundle resource = ResourceBundle.getBundle("forward");
		UrlForward url = new UrlForward();
		//操作
		Xmlbuilder xb = new Xmlbuilder();
		String para1 = "";
		String para = "";
		para1 =bean.getPara();
		para = new String(para1.getBytes("ISO-8859-1"),"GB2312");
		log.info(para);
		Integer i = 2;
		if("release".equals(request.getParameter("confirm"))){
			log.info("release:"+request.getParameter("confirm"));
			url.forward(resource.getString("release2")+bean.getPara(), request, response);		
		}else if(xb.compareFile("M-"+para+".xml")){
			try {
				if(xb.putInXml(i.toString(),request,para)){
					//生成文件成功，在页面发出提示然后转向设定模型的首页
					url.forward(resource.getString("release")+bean.getPara(), request, response);
					log.info("//生成文件成功，在页面发出提示然后转向设定模型的首页");
					log.info("这个是插入");
					request.getSession().removeAttribute("modelBean");
				}else{
					//失败
					url.forward(resource.getString("release1")+bean.getPara(), request, response);
					log.info("//失败");
					request.getSession().removeAttribute("modelBean");
				}
			} catch (DocumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}else {
			String id = "";
			try {
				id = xb.FindId(para);
				log.info(id);
				log.info("try!!!!");
			} catch (DocumentException e) {
				log.error("error try!!!",e);
			}
			if(xb.xmlCreat(id, request,para)){
				//生成文件成功，在页面发出提示然后转向设定模型的首页
				url.forward(resource.getString("release")+bean.getPara(), request, response);
				log.info("//生成文件成功，在页面发出提示然后转向设定模型的首页");
				log.info("这个是生成");
				request.getSession().removeAttribute("modelBean");
			}else{
				url.forward(resource.getString("release1")+bean.getPara(), request, response);
				log.info("XML 文件生成失败！！！");
				request.getSession().removeAttribute("modelBean");
			}
		}
		//操作完成
		
		//清session
		
		
		
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
